package com.digitalchina.lxnsy.viewholder;

import androidx.recyclerview.widget.GridLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.entity.FarmingRecordVO;
import com.digitalchina.lxnsy.interfaces.IMyNhbPos;
import com.digitalchina.lxnsy.toolkit.JointImageUrl;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.lzy.ninegrid.ImageInfo;
import com.lzy.ninegrid.NineGridView;
import com.lzy.ninegrid.preview.NineGridViewClickAdapter;

import java.util.ArrayList;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.viewholder
 * 创建人：郑皓
 * 创建时间：2018/8/28 20:10
 * 修改备注：
 * ================================================
 */

public class FarmingRecordFertilizerHolderV2 extends BaseViewHolder<FarmingRecordVO> {

    private LinearLayout layoutMore;
    private TextView tvTime;
    private TextView tvType;
    private TextView tvWeather;
    private TextView tvWhq;

    private LinearLayout layoutZK;
    private LinearLayout layoutAddWater;
    private RelativeLayout layoutSQ;

    private LinearLayout layoutZKF;
    private LinearLayout layoutNoWater;
    private RelativeLayout layoutSQF;

    private NineGridView nineGridView;
    private TextView tvMsgInfo;


    private TextView tvMu; // 亩

    private TextView tvEdit;
    private TextView tvDelete;


    private TextView tvOperation;
    private IMyNhbPos callBack;

    private EasyRecyclerView recyclerView;
    private RecyclerArrayAdapter adapter;


    public FarmingRecordFertilizerHolderV2(ViewGroup parent, IMyNhbPos callBack) {
        super(parent, R.layout.item_farming_record_v2);
        layoutMore = $(R.id.layout_more);
        tvTime = $(R.id.tv_time);
        tvType = $(R.id.tv_type);
        tvWeather = $(R.id.tv_weather);
        tvWhq = $(R.id.tv_whq);


        layoutZK = $(R.id.layout_zhankai);
        layoutSQ = $(R.id.layout_shouqi);

        layoutZKF = $(R.id.layout_zhankai_fl);
        layoutSQF = $(R.id.layout_shouqi_fl);

        layoutAddWater = $(R.id.layout_add_water);
        layoutNoWater = $(R.id.layout_no_water);

        nineGridView = $(R.id.nineGrid);
        tvMsgInfo = $(R.id.tv_msg_info);

        tvMu = $(R.id.tv_mu);

        tvEdit = $(R.id.tv_edit);
        tvDelete = $(R.id.tv_delete);

        tvOperation = $(R.id.tv_operation);
        recyclerView = $(R.id.recyclerView);

        this.callBack = callBack;
    }

    @Override
    public void setData(final FarmingRecordVO data) {
        super.setData(data);

        // 公共部分
        tvTime.setText(data.getUpdateTime() + "");
        tvWeather.setText("天气：" + data.getWeather() + " " + data.getWeatherDescribe());
        // 物候期
        tvWhq.setText(data.getPhaseNameShow() + "");
        // 补充说明
        if (StringUtil.isStrEmpty(data.getSpecification())) {
            tvMsgInfo.setVisibility(View.GONE);
        } else {
            tvMsgInfo.setVisibility(View.VISIBLE);
            tvMsgInfo.setText(data.getSpecification() + "");
        }

        tvMu.setText("亩数：" + data.getPerMuTotal() + " 亩");

        // 图片展示
        if (data.getFarmingOperationsimgs() != null && data.getFarmingOperationsimgs().size() > 0) {
            nineGridView.setVisibility(View.VISIBLE);
            ArrayList<ImageInfo> imageInfo = new ArrayList<>();

            for (int i = 0; i < data.getFarmingOperationsimgs().size(); i++) {
                ImageInfo info = new ImageInfo();
                info.setThumbnailUrl(JointImageUrl.smallImageUrl(data.getFarmingOperationsimgs().get(i).getImage()));
                info.setBigImageUrl(JointImageUrl.bigImageUrl(data.getFarmingOperationsimgs().get(i).getImage()));
                imageInfo.add(info);
            }
            nineGridView.setAdapter(new NineGridViewClickAdapter(getContext(), imageInfo));

        } else {
            nineGridView.setVisibility(View.GONE);
        }


        // 类型为农药化肥

        if (data.getIspesticideFertilizers() != null && data.getIspesticideFertilizers().size() > 0) {
            layoutSQ.setVisibility(View.VISIBLE);
            layoutZK.setVisibility(View.GONE);
        } else {
            layoutSQ.setVisibility(View.GONE);
            layoutZK.setVisibility(View.GONE);
        }

        if (data.getNotpesticideFertilizers() != null && data.getNotpesticideFertilizers().size() > 0) {
            layoutSQF.setVisibility(View.VISIBLE);
            layoutZKF.setVisibility(View.GONE);
        } else {
            layoutSQF.setVisibility(View.GONE);
            layoutZKF.setVisibility(View.GONE);
        }

        //已经展开
        layoutZK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                layoutZK.setVisibility(View.GONE);
                layoutSQ.setVisibility(View.VISIBLE);
            }
        });
        //收起
        layoutSQ.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                layoutZK.setVisibility(View.VISIBLE);
                layoutSQ.setVisibility(View.GONE);

                layoutAddWater.removeAllViews();
                layoutAddWater.removeAllViewsInLayout();
                for (int i = 0; i < data.getIspesticideFertilizers().size(); i++) {
                    View view = View.inflate(getContext(), R.layout.item_farming_add_water, null);
                    TextView tvName = view.findViewById(R.id.tv_name);
                    TextView tvUse = view.findViewById(R.id.tv_use);
                    TextView tvMultiple = view.findViewById(R.id.tv_multiple);
                    TextView tvWater = view.findViewById(R.id.tv_water);

                    tvName.setText(data.getIspesticideFertilizers().get(i).getName());
                    tvUse.setText("用量：" + data.getIspesticideFertilizers().get(i).getConsumption() + " " + data.getIspesticideFertilizers().get(i).getUnit());
                    tvMultiple.setText("稀释倍数：" + data.getIspesticideFertilizers().get(i).getMultiple() + " 倍");
                    tvWater.setText("兑水：" + data.getIspesticideFertilizers().get(i).getWaterScale() + " 斤");
                    layoutAddWater.addView(view);
                }
            }
        });
        //已经展开-无需兑水
        layoutZKF.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                layoutZKF.setVisibility(View.GONE);
                layoutSQF.setVisibility(View.VISIBLE);
            }
        });
        //收起-无需兑水
        layoutSQF.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                layoutZKF.setVisibility(View.VISIBLE);
                layoutSQF.setVisibility(View.GONE);

                layoutNoWater.removeAllViews();
                layoutNoWater.removeAllViewsInLayout();
                for (int i = 0; i < data.getNotpesticideFertilizers().size(); i++) {
                    View view = View.inflate(getContext(), R.layout.item_farming_no_water, null);
                    TextView tvName = view.findViewById(R.id.tv_name);
                    TextView tvUse = view.findViewById(R.id.tv_use);

                    tvName.setText(data.getNotpesticideFertilizers().get(i).getName());
                    tvUse.setText("用量：" + data.getNotpesticideFertilizers().get(i).getConsumption() + " kg");
                    layoutNoWater.addView(view);
                }
            }
        });


        if (data.getFarmingOperations() != null) {
            recyclerView.setVisibility(View.VISIBLE);
            GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 4);
            recyclerView.setLayoutManager(gridLayoutManager);
            adapter = new RecyclerArrayAdapter(getContext()) {
                @Override
                public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                    return new FarmingNameHolder(parent);
                }
            };
            recyclerView.setAdapter(adapter);


            adapter.clear();
            adapter.addAll(data.getFarmingOperations());

        }else{
            recyclerView.setVisibility(View.GONE);
        }


        tvEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                callBack.edit(getAdapterPosition());
            }
        });

        tvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                callBack.del(getAdapterPosition());
            }
        });
    }

}
